package com.example.warehouse_manager.controller;


import com.example.warehouse_manager.dto.AssignAuthDto;
import com.example.warehouse_manager.entity.CurrentUser;
import com.example.warehouse_manager.entity.Result;
import com.example.warehouse_manager.entity.Role;
import com.example.warehouse_manager.page.Page;
import com.example.warehouse_manager.service.RoleService;
import com.example.warehouse_manager.utils.TokenUtils;
import com.example.warehouse_manager.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private TokenUtils tokenUtils;

    //查询所有角色
    @RequestMapping("/role-list")
    public Result roleLIst(){
        List<Role> roles = roleService.queryAllRole();
        return Result.ok(roles);
    }

    //分页查询角色
    @RequestMapping("/role-page-list")
    public Result roleListPage(Page page,Role role){
        page = roleService.queryRolePage(page, role);
        return Result.ok(page);
    }

    //添加角色
    @RequestMapping("/role-add")
    public Result addRole(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //拿到当前登录的用户id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int creatBy = currentUser.getUserId();

        role.setCreateBy(creatBy);

        return roleService.saveRole(role);
    }

    //启用或禁用角色
    @RequestMapping("/role-state-update")
    public Result updateRoleState(@RequestBody Role role){
        return roleService.setRoleState(role);
    }

    //删除角色
    @RequestMapping("/role-delete/{roleId}")
    public Result deleteRole(@PathVariable Integer roleId){
        return roleService.deleteRoleById(roleId);
    }

    //根据角色id查询角色分配的所有菜单权限id
    @RequestMapping("/role-auth")
    public Result roleAuth(Integer roleId){
        List<Integer> authList = roleService.queryRoleAuthIds(roleId);
        return Result.ok(authList);
    }

    //给角色分配菜单权限
    @RequestMapping("/auth-grant")
    public Result grantAuth(@RequestBody AssignAuthDto assignAuthDto){
        roleService.saveRoleAuth(assignAuthDto);
        return Result.ok("权限分配成功");
    }

    //修改角色
    @RequestMapping("/role-update")
    public Result updateAuth(@RequestBody Role role,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME)String token){
        //拿到当前登录的用户id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int updateBy = currentUser.getUserId();

        role.setUpdateBy(updateBy);

        Result result = roleService.setROleByRid(role);
        return result;
    }

    //导出数据
    @RequestMapping("/exportTable")
    public Result exportTable(Page page, Role role){
        page = roleService.queryRolePage(page,role);
        List<?> resultList = page.getResultList();
        //响应
        return Result.ok(resultList);
    }


}
